package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.myapplication.Fragment.Discovery;
import com.example.myapplication.Fragment.Games;
import com.example.myapplication.Fragment.Home;
import com.example.myapplication.Fragment.Mine;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Home home;
    private Discovery discovery;
    private Games games;
    private Mine mine;

    //底部按钮
    private RadioGroup tab_menu;
    private RadioButton home_button;
    private RadioButton discovery_button;
    private RadioButton games_button;
    private RadioButton mine_button;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        tab_menu = (RadioGroup)findViewById(R.id.tab_menu);
        home_button = (RadioButton)findViewById(R.id.rb_bottom_home_main);
        discovery_button = (RadioButton)findViewById(R.id.rb_bottom_discovery_main);
        games_button = (RadioButton)findViewById(R.id.rb_bottom_games_main);
        mine_button = (RadioButton)findViewById(R.id.rb_bottom_mine_main);

        home_button.setOnClickListener(this);
        discovery_button.setOnClickListener( this);
        games_button.setOnClickListener(this);
        mine_button.setOnClickListener(this);



        //第一次初始化首页默认显示第一个fragment
        initFragment1();


    }


    //显示第一个fragment
    private void initFragment1(){
        //开启事务，fragment的控制是由事务来实现的
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        //第一种方式（add），初始化fragment并添加到事务中，如果为null就new一个
        if(home == null){
            home = new Home("111");
            transaction.add(R.id.main_frame_layout, home);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(home);

        //提交事务
        transaction.commit();
    }

    //显示第二个fragment
    private void initFragment2(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(discovery == null){
            discovery = new Discovery("发现");
            transaction.add(R.id.main_frame_layout,discovery);
        }
        hideFragment(transaction);
        transaction.show(discovery);

        transaction.commit();
    }

    //显示第三个fragment
    private void initFragment3(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(games == null){
            games = new Games("游戏");
            transaction.add(R.id.main_frame_layout,games);
        }
        hideFragment(transaction);
        transaction.show(games);


        transaction.commit();
    }

    //显示第四个fragment
    private void initFragment4(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(mine == null){
            mine = new Mine("我的");
            transaction.add(R.id.main_frame_layout,mine);
        }
        hideFragment(transaction);
        transaction.show(mine);


        transaction.commit();
    }
    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction){
        if(home != null){
            transaction.hide(home);
        }
        if(discovery != null){
            transaction.hide(discovery);
        }
        if(games != null){
            transaction.hide(games);
        }
        if(mine != null){
            transaction.hide(mine);
        }
    }



    public void onClick(View v) {
        if(v == home_button){
            initFragment1();
        }else if(v == discovery_button){
            initFragment2();
        }else if(v == games_button){
            initFragment3();
        }else if(v == mine_button){
            initFragment4();
        }
    }


}
